package pwd.allen.aop.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 环绕通知：可在目标方法调用之前和之后执行，可以修改返回结果
 *
 * @author 门那粒沙
 * @create 2020-08-30 12:31
 **/
public class MyMethodInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Method method = invocation.getMethod();
        Object[] args = invocation.getArguments();
        System.out.println(String.format("【MethodInterceptor】Before '%s',args = %s", method.getName(), Arrays.toString(args)));
        Object returnValue = invocation.proceed();
        System.out.println(String.format("【MethodInterceptor】After '%s',args = %s,return = %s", method.getName(), Arrays.toString(args), returnValue));
        return returnValue;
    }
}
